<?php
	if(isset($_REQUEST['CallSid'])){
		$status = $_REQUEST['CallStatus'];
		$duration = $_REQUEST['Duration'];
		$user_email = $_REQUEST['email'];
	
		/*if($status=="no-answer"){
			mail("miqdad@quintetsolutions.com","Twilio Call","No Answer");
		}
		elseif($status=="busy"){
			mail("miqdad@quintetsolutions.com","Twilio Call","Busy");
		}
		elseif($status=="in-progress"){
			mail("miqdad@quintetsolutions.com","Twilio Call","in-progress");
		}
		elseif($status=="completed"){
			mail("miqdad@quintetsolutions.com","Twilio Call","completed");
		}
		else{
			mail("miqdad@quintetsolutions.com","Twilio Call","Unknown");
		}
		*/
		$text = "";
		foreach($_REQUEST as $key=>$r){
			$text .="$key : $r\n";
		}
		$headers = "From: Singlehop Leap3<singlehop@qlabz.com>" . "\r\n";
		mail("miqdad@quintetsolutions.com","Singlehop Leap3 Twilio Returns.","$text",$headers);
		
		if($status!="completed" or $duration==0){
			$mailSent = sendMail($user_email);
		}
	}
	
	
	
	function sendMail($user_email){
		
		$from_email = "leap3@singlehop.com";
		$from_name = "Singlehop Leap3";
		$excludes = array("hotmail.com","live.com","gmail.com","yahoo.com","aol.com");
		
		$activation_link = "http://qlabz.com/miqdad/twilio/activate.php?email=$user_email";
		
		$split = explode("@",$user_email);
		$email_domain = $split[1];
		if(!in_array($email_domain,$excludes)){
			// Always set content-type when sending HTML email
			// To send HTML mail, the Content-type header must be set
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			
			// Additional headers
			$headers .= "From: $from_name <$from_email>" . "\r\n";
			
			$activationLink = "<a href='$activation_link'>Click this link </a>";
			
			$message = "Hi,<br /> Singlehop received a request for a free LEAP 3 account.To approve and activate this account Please $activationLink";
			
			$mail = mail($user_email,"Activate Singlehop LEAP 3 account","$message",$headers);
			if($mail){
				return "Mail has been sent. :-)";
			}
			return "Mail Sending failed :-(";
		}
	}
?>